package com.lighting.smssystem.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lighting.smssystem.aspect.RequireRole;
import com.lighting.smssystem.constrants.UserRole;
import com.lighting.smssystem.model.bo.FavoriteList;
import com.lighting.smssystem.model.vo.CompanyFilter;
import com.lighting.smssystem.model.vo.CompanyFilterNew;
import com.lighting.smssystem.model.vo.FavoriteForm;
import com.lighting.smssystem.service.IFavoriteService;
import com.lighting.smssystem.utils.BaseResponse;
import com.lighting.smssystem.utils.ResponseUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Author LeeMaster、
 *
 * 用户收藏夹的 增删改查
 */
@RestController
@RequestMapping("/favorite")
@Api(
        value = "用户收藏夹信息接口",
        protocols = "http",
        description = "管理用户收藏夹信息接口的API"
)
@Slf4j
public class FavoriteController {

    @Resource
    IFavoriteService favoriteService;


    @ApiOperation(
            value = "分页查询用户收藏夹接口",
            httpMethod = "GET",
            authorizations = @Authorization(
                    "VIP 用户"
            )
    )
    @GetMapping("")
    @ApiResponse(code = 200,message = "",response = FavoriteList.class)
    @RequireRole(role = UserRole.VIP)
    public BaseResponse listFavorites(
            @ApiParam CompanyFilterNew filter
    ){
        log.info("查询用户最爱{}", filter.toString());

        return ResponseUtil.success(favoriteService.listUserFavorite(filter));
    }

    @ApiOperation(
            value = "添加收藏接口",
            httpMethod = "POST",
            authorizations = @Authorization(
                    "VIP 用户"
            )
    )
    @PostMapping("")
    @RequireRole(role = UserRole.VIP)
    public BaseResponse addFavorite(
            @ApiParam(name = "更新收藏夹表单",type = "json") @RequestBody FavoriteForm form
    ){
        log.info("添加用户收藏{}", form.toString());
        return ResponseUtil.success(favoriteService.addFavorite(form));
    }

    @ApiOperation(
            value = "删除收藏接口",
            httpMethod = "DELETE",
            authorizations = @Authorization(
                    "VIP 用户"
            )
    )
    @DeleteMapping("")
    @RequireRole(role = UserRole.VIP)
    public BaseResponse deleteFavorities(
            @ApiParam @RequestBody()Map<String, Object> data
            ){
        log.info("取消收藏{},,{}", data.get("id"), data.get("ids"));
        return ResponseUtil.success(favoriteService.delFavorite(Long.parseLong(data.get("id").toString()), data.get("ids").toString()));
    }
}
